<template>
    <div class="search">
        <el-autocomplete @select="goDetail" :trigger-on-focus="false" clearable placeholder="请您输入医院名称" v-model="hosname"
            :fetch-suggestions="fetchData" />
        <el-button type="primary" size="default" :icon="Search">搜索</el-button>
    </div>
</template>

<script setup lang="ts">
import { Search } from '@element-plus/icons-vue'
import { useRouter } from 'vue-router';
import { ref } from 'vue';
import { reqHospitalInfo } from '@/api/home/index'
let $router = useRouter()
// 收集搜索的关键字
let hosname = ref('')
// input框的回调
const fetchData = async (keyword: any, cb: any) => {
    // 当用户输入完关键字函数会执行一次，发请求获取数据即可
    let result = await reqHospitalInfo(keyword)
    // 整理数据，变成element-plus组件所需要的数据格式
    let showData = result.data.map((item: any) => {
        return {
            value: item.hosname, //展示医院的名字
            hoscode: item.hoscode //存储医院的编码
        }
    })
    // 给组件提供展示的数据
    cb(showData)
}

// 点击某一个推荐医院的回调
const goDetail = (item: any) => {
    $router.push({ path: '/hospital/register', query: { hoscode: item.hoscode } })
}
</script>

<style scoped lang="scss">
.search {
    width: 100%;
    height: 50px;
    display: flex;
    justify-content: center;
    align-items: center;
    margin: 10px 0;

    ::v-deep(.el-input__wrapper) {
        width: 600px;
        margin-right: 10px;
    }
}
</style>